package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.BuSysPerson;
import com.ruoyi.common.core.domain.entity.BusinessSys;
import com.ruoyi.common.core.domain.entity.Person;
import com.ruoyi.common.core.domain.entity.SealTempjur;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.service.IBusinessSysService;
import com.ruoyi.system.service.IPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BuSysPersonMapper;
import com.ruoyi.system.service.IBuSysPersonService;

/**
 * 业务系统对应人员信息Service业务层处理
 *
 * @author ess
 * @date 2021-10-18
 */
@Service
public class BuSysPersonServiceImpl implements IBuSysPersonService
{
    @Autowired
    private BuSysPersonMapper buSysPersonMapper;

    @Autowired
    private IBusinessSysService businessSysService;

    @Autowired
    private IPersonService personService;

    /**
     * 查询业务系统对应人员信息
     *
     * @param buPerId 业务系统对应人员信息主键
     * @return 业务系统对应人员信息
     */
    @Override
    public BuSysPerson selectBuSysPersonByBuPerId(String buPerId) {


        BuSysPerson buSysPerson = buSysPersonMapper.selectBuSysPersonByBuPerId(buPerId);
        if (buSysPerson != null) {
            //               页面展示本系统人 姓名-生日后四位
            Person person = personService.selectPersonByPersonId(buSysPerson.getPersonId());
            buSysPerson.setSysPersonName(person.getPersonName() + "-" + person.getBirNum().substring(4));
        }
        return buSysPerson;
    }

    /**
     * 查询业务系统对应人员信息列表
     *
     * @param buSysPerson 业务系统对应人员信息
     * @return 业务系统对应人员信息
     */
    @Override
    public List<BuSysPerson> selectBuSysPersonList(BuSysPerson buSysPerson) {
        ArrayList<BuSysPerson> buSysPersonArrayList = new ArrayList<>();

        if (StringUtils.isNotEmpty(buSysPerson.getPersonId())) {
            List<Person> personList = personService.selectPersonListByPersonName(buSysPerson.getPersonId());
            if (personList != null && personList.size() > 0) {
                for (Person person : personList) {
                    buSysPerson.setPersonId(person.getPersonId());
                    List<BuSysPerson> buSysPeople = buSysPersonMapper.selectBuSysPersonList(buSysPerson);
                    if (buSysPeople != null && buSysPeople.size() > 0) {
                        buSysPersonArrayList.addAll(buSysPeople);
                    }
                }
            }

            return takeBuSysPersonReturn(buSysPersonArrayList);
        }

        return takeBuSysPersonReturn(buSysPersonMapper.selectBuSysPersonList(buSysPerson));
    }

    /**
     * 新增业务系统对应人员信息
     *
     * @param buSysPerson 业务系统对应人员信息
     * @return 结果
     */
    @Override
    public int insertBuSysPerson(BuSysPerson buSysPerson) {
//        主键uuid
        buSysPerson.setBuPerId(UUID.randomUUID().toString().replace("-", ""));

        return buSysPersonMapper.insertBuSysPerson(buSysPerson);
    }

    /**
     * 修改业务系统对应人员信息
     *
     * @param buSysPerson 业务系统对应人员信息
     * @return 结果
     */
    @Override
    public int updateBuSysPerson(BuSysPerson buSysPerson) {
        return buSysPersonMapper.updateBuSysPerson(buSysPerson);
    }

    /**
     * 批量删除业务系统对应人员信息
     *
     * @param buPerIds 需要删除的业务系统对应人员信息主键
     * @return 结果
     */
    @Override
    public int deleteBuSysPersonByBuPerIds(String[] buPerIds) {
        return buSysPersonMapper.deleteBuSysPersonByBuPerIds(buPerIds);
    }

    /**
     * 删除业务系统对应人员信息信息
     *
     * @param buPerId 业务系统对应人员信息主键
     * @return 结果
     */
    @Override
    public int deleteBuSysPersonByBuPerId(String buPerId) {
        return buSysPersonMapper.deleteBuSysPersonByBuPerId(buPerId);
    }

    /**
     * 校验业务系统id唯一
     *
     * @param buSysPerId
     * @return
     */
    @Override
    public String checkBuSysPerUnique(String buSysPerId) {
        BuSysPerson buSysPerson = buSysPersonMapper.selectBusysByBuSysPerId(buSysPerId);
        if (buSysPerson != null) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验personid 唯一
     *
     * @param personId
     * @return
     */
    @Override
    public String checkBuSysPerUniqueByPersonId(String personId) {
        BuSysPerson buSysPerson = buSysPersonMapper.selectBusysByPeronId(personId);
        if (buSysPerson != null) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public BuSysPerson selectBusysByBuSysPerId(String buSysPerId) {
        return buSysPersonMapper.selectBusysByBuSysPerId(buSysPerId);
    }


    private List<BuSysPerson> takeBuSysPersonReturn(List<BuSysPerson> buSysPeople) {
        if (buSysPeople != null && buSysPeople.size() > 0) {
            for (BuSysPerson buSysPerson : buSysPeople) {
                BusinessSys businessSys = businessSysService.selectBusinessSysByBSysId(buSysPerson.getSysId());
                buSysPerson.setSysName(businessSys.getbName());

//               页面展示本系统人 姓名-生日后四位
                Person person = personService.selectPersonByPersonId(buSysPerson.getPersonId());
                buSysPerson.setSysPersonName(person.getPersonName() + "-" + person.getBirNum().substring(4));
            }

        }

        return buSysPeople;
    }
}
